<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="规则名称中文名称" field="rule_name_zh">
            <a-input v-model="searchForm.rule_name_zh" placeholder="请输入规则名称中文名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="规则名称英文名称" field="rule_name_en">
            <a-input v-model="searchForm.rule_name_en" placeholder="请输入规则名称英文名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="规则名称日语名称" field="rule_name_ja">
            <a-input v-model="searchForm.rule_name_ja" placeholder="请输入规则名称日语名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="规则名称韩文名称" field="rule_name_ko">
            <a-input v-model="searchForm.rule_name_ko" placeholder="请输入规则名称韩文名称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="规则类型" field="rule_type">
            <sa-select v-model="searchForm.rule_type" dict="rulesType" placeholder="请选择规则类型" allow-clear />
          </a-form-item>
        </a-col>
      </template>
	  <template #rule_sort="{ record }">
	          <a-input-number 
	            v-model="record.rule_sort"
	            :min="0"
	            style="width: 80px"
	            @change="changeSortOrder(record.rule_id, record.rule_sort)"
	          />
	  </template>
	  <template #operationBeforeExtend="{ record }">
	    <a-link @click="openAdd(record)" v-if="record.parent_id == 0 "  v-auth="['/matchingRule/MatchingRule/save']"> <icon-plus /> 新增 </a-link>
	  </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/rule'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  rule_name_zh: '',
  rule_name_en: '',
  rule_name_ja: '',
  rule_id: '',
  rule_name_ko: '',
  rule_type: '',
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  pk: 'rule_id',
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  isExpand: true,
  add: {
    show: true,
    auth: ['/matchingRule/MatchingRule/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/matchingRule/MatchingRule/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/matchingRule/MatchingRule/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/matchingRule/MatchingRule/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/matchingRule/MatchingRule/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})
const openAdd = (record) => {
  editRef.value?.open()
  editRef.value?.setFormData({ parent_id: record.rule_id,rule_level:1,rule_type:record.rule_type })
}
// SaTable 列配置
const columns = reactive([
  { title: '规则名称中文名称', dataIndex: 'rule_name_zh', width: 150 },
  { title: '规则名称英文名称', dataIndex: 'rule_name_en', width: 150 },
  { title: '规则名称日语名称', dataIndex: 'rule_name_ja', width: 150 },
  { title: '规则名称韩文名称', dataIndex: 'rule_name_ko', width: 150 },
  { title: '规则等级', dataIndex: 'rule_level', dict: 'rulesLevel', width: 100 },
  { title: '规则类型', dataIndex: 'rule_type', dict: 'rulesType', width: 100 },
  {
     title: '规则排序', 
     dataIndex: 'rule_sort', 
     width: 100,
     slots: { customRender: 'rule_sort' } // 新增插槽配置
   },
   { title: '是否必填', dataIndex: 'is_required',dict:'yes_or_no', width: 100 },
  { title: '创建时间', dataIndex: 'create_time', width: 100 },
  // { title: '父级', dataIndex: 'parent_id_text', width: 180 },
  
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}
const changeSortOrder = async (videoId, sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: videoId,
      rule_sort: sortOrder
    });
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.error('修改排序出错:', error);
    Message.error('排序修改失败');
  }
}
// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
